package com.example.sensors;

import java.util.Map;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.example.singletones.contextObj;

public class Sensor_GPS extends AbstractSensor implements LocationListener {
	public static final String NAME = "GPS";
	private LocationManager lm;

	public Sensor_GPS() {
		lm = (LocationManager) contextObj.getInstance().getmContext()
				.getSystemService(Context.LOCATION_SERVICE);
		// enableSensor();
		//
		// new Timer().scheduleAtFixedRate(new java.util.TimerTask() {
		//
		// @Override
		// public void run() {
		// shouldTurnOffGPS++;
		// if (shouldTurnOffGPS > 100) {
		// disableSensor();
		// }
		// }
		// }, 0, 500);

	}

	public void disableSensor() {
		boolean isGPSEnabled = isSensorEnabled;
		super.disableSensor();
		if (isGPSEnabled) {
			isGPSEnabled = false;
			lm.removeUpdates(this);
		}

	}

	public void enableSensor() {
		boolean isGPSEnabled = isSensorEnabled;
		super.enableSensor();
		if (isGPSEnabled == false) {
			lm = (LocationManager) contextObj.getInstance().getmContext()
					.getSystemService(Context.LOCATION_SERVICE);
			lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,
					2000.0f, this);
		}
	}

	public void onLocationChanged(Location location) {
		Map<String, Object> eventArgs = initEventArgs();
		eventArgs.put("eventType", "sensorInfoReceived");
		eventArgs.put("location", location);
		OnSensorChanged(eventArgs);
	}

	public void onProviderDisabled(String arg0) {
		disableSensor();
	}

	public void onProviderEnabled(String arg0) {
		enableSensor();
	}

	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
	}

	public String getSensorName() {
		return "GPS";
	}

}
